/* eslint-disable @typescript-eslint/no-use-before-define */
import { DictValueEnum } from "@/components/DictTag";
import { genUCC } from "@/services/api/Tools";
import { getDict } from "@/services/api/module/system/dict";
import { getRole } from "@/services/api/module/system/role";
import { getTenant } from "@/services/api/module/tenant/tenant/tenants";
import {
	DrawerForm,
	ModalForm,
	ProFormDigit,
	ProFormInstance,
	ProFormRadio,
	ProFormSelect,
	ProFormText,
	ProFormTextArea
} from "@ant-design/pro-components";
import { Button, Col, Row } from "antd";
import React, { useEffect, useRef, useState } from "react";
export type TenantFormData = Record<string, unknown> & Partial<API.Tenant.TenantBo>;
export type TenantFormProps = {
	onCancel: () => void;
	onSubmit: (values: TenantFormData) => Promise<any>;
	open: boolean;
	id: number | undefined;
	statusOptions: DictValueEnum;
	industryOptions: DictValueEnum;
	scaleOptions: DictValueEnum;
};
const UpdateTenantForm: React.FC<TenantFormProps> = props => {
	// 国际化
	const formRef = useRef<ProFormInstance>();
	const { statusOptions, industryOptions, scaleOptions, id, onCancel, onSubmit, open } = props;
	// 初始化
	useEffect(() => {
		formRef?.current?.resetFields();
		if (id) {
			setFormValue(id);
		}
	}, [id]);
	const [data, setData] = useState<any>();
	// 表单赋值
	const setFormValue = (id: number) => {
		getTenant(id).then(res => {
			setData(res);
			formRef?.current?.setFieldsValue(res);
		});
	};
	// 关闭
	const handleCancel = () => {
		setData({});
		onCancel();
	};
	// 表单提交
	const handleFinish = async (values: TenantFormData) => {
		return onSubmit({
			...values,
			id: id,
			tenantId: data?.tenantId
		} as TenantFormData);
	};
	const generatedUcc = () => {
		genUCC().then((data: any) => {
			// @ts-ignore
			formRef?.current?.setFieldsValue({ licenseNumber: data });
		});
	};
	return (
		<DrawerForm<TenantFormData>
			width={700}
			title={"租户修改"}
			open={open}
			formRef={formRef}
			grid={true}
			layout="vertical"
			drawerProps={{
				destroyOnClose: true,
				onClose: () => {
					handleCancel();
				}
			}}
			onFinish={handleFinish}
		>
			<ProFormText
				name="companyName"
				colProps={{ md: 12, xl: 12 }}
				label="企业名称"
				placeholder="请输入企业名称"
				rules={[
					{
						required: true,
						message: "请输入企业名称！"
					}
				]}
			/>
			<ProFormText
				colProps={{ md: 12, xl: 12 }}
				name="licenseNumber"
				fieldProps={{
					suffix: (
						<Button type="link" size="small" onClick={generatedUcc}>
							生成
						</Button>
					)
				}}
				label="统一社会信用代码"
				rules={[
					{
						required: true,
						message: "请输入统一社会信用代码"
					}
				]}
				placeholder="请输入统一社会信用代码"
			/>
			<ProFormSelect
				colProps={{ md: 12, xl: 12 }}
				label="行业"
				valueEnum={industryOptions}
				name="industry"
				rules={[
					{
						required: true,
						message: "请选择行业"
					}
				]}
			/>
			<ProFormSelect
				colProps={{ md: 12, xl: 12 }}
				label="规模"
				valueEnum={scaleOptions}
				name="scale"
				rules={[
					{
						required: true,
						message: "请选择规模"
					}
				]}
			/>
			<ProFormText
				colProps={{ md: 12, xl: 12 }}
				name="contactRealName"
				label="联系人"
				placeholder="请输入联系人"
				rules={[
					{
						required: true,
						message: "请输入联系人"
					}
				]}
			/>
			<ProFormText
				colProps={{ md: 12, xl: 12 }}
				name="contactPhone"
				label="联系人电话"
				placeholder="请输入联系人电话"
				rules={[
					{
						required: true,
						message: "请输入联系人电话"
					}
				]}
			/>
			<ProFormText
				colProps={{ md: 12, xl: 12 }}
				name="contactEmail"
				label="联系邮箱"
				placeholder="请输入联系邮箱"
				rules={[
					{
						required: true,
						message: "请输入联系邮箱"
					},
					{
						type: "email",
						message: "邮箱格式不正确"
					}
				]}
			/>
			<ProFormText
				colProps={{ md: 12, xl: 12 }}
				name="telephone"
				label="座机电话"
				placeholder="请输入联系邮箱"
				rules={[
					{
						required: true,
						message: "请输入联系邮箱"
					}
				]}
			/>
			<ProFormTextArea name="address" label="企业地址" placeholder="请输入企业地址" />
			<ProFormTextArea name="intro" label="企业简介" placeholder="请输入企业简介" />
		</DrawerForm>
	);
};

export default UpdateTenantForm;
